#include <QDir>
#include <QDebug>
#include "analyse.h"


void analyse::analy(const QString &path1, const QString &path2, Ui::FcSync *ui){
    QStringList File1, File2;
    File1.append(getFile(path1, NULL));
    File2.append(getFile(path2, NULL));
    int cnt = 1;
    for (int i = 0; i < File1.size(); i++) {
        if (File2.contains(File1[i])) {
            if (md5.getFileMd5(path1+File1[i]) != md5.getFileMd5(path2+File1[i])) {
                QTreeWidgetItem *newltem = new QTreeWidgetItem();
                newltem->setText(0, QString::number(cnt++));
                QFileInfo file1(path1+File1[i]);
                QFileInfo file2(path2+File1[i]);
                newltem->setText(1, File1[i]);
                newltem->setText(2, QString::number(file1.size()));
                newltem->setText(3, file1.lastModified().toString());
                newltem->setText(4, ">>");
                newltem->setText(5, "diff");
                newltem->setText(6, "<<");
                newltem->setText(7, File1[i]);
                newltem->setText(8, QString::number(file2.size()));
                newltem->setText(9, file2.lastModified().toString());
                for (int j = 0; j <= 9; j++)
                    newltem->setTextColor(j, "Red");
                ui->treeWidget->addTopLevelItem(newltem);
            }
        }
    }
    for (int i = 0; i < File1.size(); i++) {
        if (File2.contains(File1[i])) {
            if (md5.getFileMd5(path1+File1[i]) == md5.getFileMd5(path2+File1[i])) {
                QTreeWidgetItem *newltem = new QTreeWidgetItem();
                newltem->setText(0, QString::number(cnt++));
                QFileInfo file1(path1+File1[i]);
                QFileInfo file2(path2+File1[i]);
                newltem->setText(1, File1[i]);
                newltem->setText(2, QString::number(file1.size()));
                newltem->setText(3, file1.lastModified().toString());
                newltem->setText(5, "same");
                newltem->setText(7, File1[i]);
                newltem->setText(8, QString::number(file2.size()));
                newltem->setText(9, file2.lastModified().toString());
                for (int j = 0; j <= 9; j++)
                    newltem->setTextColor(j, "Green");
                ui->treeWidget->addTopLevelItem(newltem);
            }
        }
    }
    for (int i = 0; i < File1.size(); i++) {
        if (!File2.contains(File1[i])) {
            QTreeWidgetItem *newltem = new QTreeWidgetItem();
            newltem->setText(0, QString::number(cnt++));
            QFileInfo file1(path1+File1[i]);
            newltem->setText(1, File1[i]);
            newltem->setText(2, QString::number(file1.size()));
            newltem->setText(3, file1.lastModified().toString());
            newltem->setText(4, ">>");
            newltem->setText(5, "add");
            for (int j = 0; j <= 9; j++)
                newltem->setTextColor(j, "Blue");
            ui->treeWidget->addTopLevelItem(newltem);
        }
    }
    for (int i = 0; i < File2.size(); i++) {
        if (!File1.contains(File2[i])) {
            QTreeWidgetItem *newltem = new QTreeWidgetItem();
            newltem->setText(0, QString::number(cnt++));
            QFileInfo file2(path2+File2[i]);
            newltem->setText(5, "add");
            newltem->setText(6, "<<");
            newltem->setText(7, File2[i]);
            newltem->setText(8, QString::number(file2.size()));
            newltem->setText(9, file2.lastModified().toString());
            for (int j = 0; j <= 9; j++)
                newltem->setTextColor(j, "Blue");
            ui->treeWidget->addTopLevelItem(newltem);
        }
    }
}

QStringList analyse::getFile(const QString &root, const QString &path)
{
    if (root.isEmpty())
        return QStringList();

    QStringList fileSet, fileNames;
    QString fileName;
    QDir dir(root+path);
    QFileInfoList infoList = dir.entryInfoList(QDir::NoDotAndDotDot|QDir::Dirs|QDir::Files);
    for (int i=0; i<infoList.size(); i++)
    {
        QFileInfo info = infoList[i];
        if (info.isDir()) {
            fileSet.append(path+"/"+info.fileName());
            fileNames = getFile(root, path+"/"+info.fileName());
            fileSet.append(fileNames);
        } else {
            fileName = info.fileName();
            fileSet.append(path+"/"+fileName);
        }
    }
    return fileSet;
}
